package net;

import gui.FriendListUi;
import gui.UserUI;
import util.Protocol;

import javax.swing.*;
import java.util.HashMap;

public class ReceiveInformation implements Runnable {
    static HashMap<String, JFrame> clientPool;

    static {
        clientPool = new HashMap<>();
    }

    boolean isOk;
    Protocol protocol = new Protocol();
    BackClient backClient = new BackClient();

    public void addThisWindows(String windowName, JFrame jframe) {
        clientPool.put(windowName, jframe);
    }

    public JFrame returnThisWindows(String windowName) {
        return clientPool.get(windowName);
    }

    public boolean addFriendIsOk() {
        return isOk;
    }

    public void run() {
        String str = backClient.receiveFriendMassage();
        String[] content = str.split(":");
        String head = content[0];

        if (head.equals("AddFriendRequest")) {
            String[] name = content[1].split(",");
            String friendID = name[0];
            String nickname = name[1];

            int choose = JOptionPane.showConfirmDialog(null, friendID + "想加您为好友。");
            if (choose == JOptionPane.YES_OPTION) {
                String addmassage = protocol.getAddFriendOkMessage(nickname, friendID);
                backClient.SendMassage(addmassage);
                FriendListUi friendListUi = (FriendListUi) returnThisWindows("friendlist");
                friendListUi.setNewFriend(friendID);
            }
            if (choose == JOptionPane.NO_OPTION) {
                String addmassage = protocol.getAddFriendNoMessage(nickname, friendID);
                backClient.SendMassage(addmassage);
            }
        }

        if (head.equals("NotExist")) {
            FriendListUi friendListUi = (FriendListUi) returnThisWindows("friendlist");
            JOptionPane.showMessageDialog(null, "查询好友不存在");
        }

        if (head.equals("Refuse")) {
            JOptionPane.showMessageDialog(null, "对方拒绝添加您为好友");
        }

        if (head.equals("addFriendok")) {
            String[] name = content[1].split(",");
            String friendID = name[0];
            String nickname = name[1];
            System.out.println(nickname + 173);

            String addMessage = protocol.getAddFriendOkMessage(nickname, friendID);
            backClient.SendMassage(addMessage);
            JOptionPane.showMessageDialog(null, "发送成功");
            FriendListUi friendListUi = (FriendListUi) returnThisWindows("friendlist");
            friendListUi.setNewFriend(friendID);
        }
        if (head.equals("NewMessage")) {
            String friendID = content[1];
            String context = content[2];
            String nickname = content[3];
            UserUI frame = (UserUI) returnThisWindows(friendID);
            if (frame == null) {
                UserUI ui = new UserUI();
                ui.getFriendID(friendID);
                ui.setNickname(nickname);
                ui.setVisible(true);
                ui.setConversation(friendID, context);
                addThisWindows(friendID, ui);
            } else {
                frame.setConversation(friendID, context);
            }
        }
    }
}
	

